//
//  collectionViewController.m
//  7ZhiLe
//
//  Created by 海明洋 on 15/3/13.
//  Copyright (c) 2015年 海明洋. All rights reserved.
//

#import "collectionViewController.h"
#import "model.h"
#import "lineCell.h"
#import "SVPullToRefresh.h"

@interface collectionViewController ()
@property (nonatomic,strong) NSMutableArray *btnArr;
@property (nonatomic,strong) UITableView *tableView1;
@property (nonatomic,strong) UITableView *tableView2;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIView *selectedView;
@property (nonatomic,assign) float btn1X;
@property (nonatomic,assign) float btn2X;
@property (nonatomic,assign) float selectedViewX;
@property (nonatomic,assign) float selectedViewY;

@property (nonatomic,retain) NSMutableArray *lineArr;

@end

@implementation collectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    
    _btnArr=[[NSMutableArray alloc] init];
    _selectedView=[[UIView alloc] init];
    _selectedView.backgroundColor=[UIColor blueColor];
    _selectedView.alpha=0.5;
    
    //分割线
    UIView *line=[[UIView alloc] initWithFrame:CGRectMake(0, 80-1, [self screenSize].width, 0.5)];
    line.backgroundColor=[UIColor grayColor];
    line.alpha=0.6;
    [self.view addSubview:line];
    
#pragma mark - 滚动列表
    _scrollView=[model shareScrollView];
    _scrollView.frame=CGRectMake(0, 60, [self screenSize].width, 6.8*80);
    
    _scrollView.delegate=self;
    _scrollView.pagingEnabled=YES;
    //_scrollView.userInteractionEnabled=NO;
    _scrollView.scrollEnabled=NO;
    _scrollView.contentSize=CGSizeMake([self screenSize].width*2, 6.8*80);
    _scrollView.showsHorizontalScrollIndicator=NO;
    _scrollView.backgroundColor=[UIColor redColor];
    
    _tableView1=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.frame.size.width, _scrollView.frame.size.height) style:UITableViewStylePlain];
    _tableView1.delegate=self;
    _tableView1.dataSource=self;
    _tableView1.backgroundColor=[UIColor clearColor];
    _tableView1.showsVerticalScrollIndicator=NO;
    _tableView1.separatorColor=[UIColor blueColor];
    //_tableView1.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    //添加下拉刷新1
    __weak collectionViewController *blockSelf1 = self;
    [_tableView1 addPullToRefreshWithActionHandler:^{
        NSLog(@"下拉更新");
        //使用GCD开启一个线程，使圈圈转2秒
        int64_t delayInSeconds = 1.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            //[blockSelf refreshView];
            [blockSelf1.tableView1.pullToRefreshView stopAnimating];
        });
    }];
    
    _tableView2=[[UITableView alloc] initWithFrame:CGRectMake([self screenSize].width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height) style:UITableViewStylePlain];
    _tableView2.delegate=self;
    _tableView2.dataSource=self;
    _tableView2.backgroundColor=[UIColor clearColor];
    _tableView2.showsVerticalScrollIndicator=NO;
    _tableView2.separatorColor=[UIColor redColor];
    //_tableView2.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    //添加下拉刷新2
    __weak collectionViewController *blockSelf2 = self;
    [_tableView2 addPullToRefreshWithActionHandler:^{
        NSLog(@"下拉更新");
        //使用GCD开启一个线程，使圈圈转2秒
        int64_t delayInSeconds = 1.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            //[blockSelf refreshView];
            [blockSelf2.tableView2.pullToRefreshView stopAnimating];
        });
    }];
    
    [_scrollView addSubview:_tableView1];
    [_scrollView addSubview:_tableView2];
    [self.view addSubview:_scrollView];
    
    model *mod=[model shareModel];
    if (!mod.searchSelect) {
        [self btnAct1:nil];
        _selectedView.center=CGPointMake(_btn1X, _selectedViewY);
    } else {
        [self btnAct2:nil];
        _selectedView.center=CGPointMake(_btn2X, _selectedViewY);
    }
    
    
#pragma mark - 选项按钮
    for (int i=0; i<2; i++) {
        UIButton *button=[[UIButton alloc] init];
        
        button.frame=CGRectMake(i*[self screenSize].width/2, _scrollView.frame.origin.y+CGRectGetHeight(_scrollView.frame), [self screenSize].width/2, self.view.frame.size.height-(_scrollView.frame.origin.y+CGRectGetHeight(_scrollView.frame)));
        
        switch (i) {
            case 0:{
                _selectedView.frame=button.frame;
                _btn1X=button.center.x;
                //[button setBackgroundImage:[UIImage imageNamed:@"Q1.png"] forState:UIControlStateNormal];
                [button setTitle:@"职位收藏" forState:UIControlStateNormal];
                button.backgroundColor=[UIColor cyanColor];
                [button addTarget:self action:@selector(btnAct1:) forControlEvents:UIControlEventTouchUpInside];
            }
                break;
            case 1:{
                _btn2X=button.center.x;
                //[button setBackgroundImage:[UIImage imageNamed:@"Q2.png"] forState:UIControlStateNormal];
                [button setTitle:@"文章收藏" forState:UIControlStateNormal];
                button.backgroundColor=[UIColor greenColor];
                [button addTarget:self action:@selector(btnAct2:) forControlEvents:UIControlEventTouchUpInside];
            }
                break;
            default:
                break;
        }
        
        button.tag=100+i;
        [_btnArr addObject:button];
        [self.view addSubview:button];
    }
    
    _selectedViewX=_selectedView.center.x;
    _selectedViewY=_selectedView.center.y;
    
    //[self.view addSubview:_selectedView];
    
}

#pragma mark - 选项按钮方法
- (void)btnAct1:(UIButton *)btn
{
    [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void)btnAct2:(UIButton *)btn
{
    [_scrollView setContentOffset:CGPointMake([self screenSize].width, 0) animated:YES];
}

#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float offx=scrollView.contentOffset.x;
    if (offx>0 && offx<[self screenSize].width) {
        float offMovePercent=offx/([self screenSize].width);
        float frameCenterMove=offMovePercent*(_btn2X-_btn1X);
        //NSLog(@"frameMove=%f",frameCenterMove);
        _selectedView.center=CGPointMake(_selectedViewX+frameCenterMove, _selectedViewY);
    } else if (offx<0){
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        
    } else if (offx>[self screenSize].width){
        [_scrollView setContentOffset:CGPointMake([self screenSize].width, 0) animated:NO];
    }
    
}

#pragma mark - UITableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    model *mod=[model shareModel];
//    return mod.collectionDataArr.count;
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellStr=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellStr];
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    
    //    UIView *view=[[UIView alloc] init];
    //    view.backgroundColor=[UIColor blueColor];
    //    view.frame=CGRectMake(0, 90, cell.frame.size.width, 1);
    //    [cell.contentView addSubview:view];
    
//    UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"t1(2)@2x.png"]];
//    UIImageView *imageView=[[UIImageView alloc] initWithImage:image];
//    imageView.frame=CGRectMake(-20, 10, image.size.width*1.2, image.size.height*1.4);//7.1/12,8.7/12
//    [cell addSubview:imageView];
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)viewDidLayoutSubviews
{
    if ([_tableView1 respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView1 setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([_tableView1 respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView1 setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([_tableView2 respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView2 setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([_tableView2 respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView2 setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - 重画列表分割线
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, rect);
    //上分割线，
    CGContextSetStrokeColorWithColor(context, [UIColor  blueColor].CGColor);
    CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1));
//    //下分割线
//    CGContextSetStrokeColorWithColor(context, [UIColor  blueColor].CGColor); CGContextStrokeRect(context, CGRectMake(5, rect.size.height, rect.size.width - 10, 1));
}

//获取ios屏幕大小（去掉状态条）
- (CGSize) screenSize
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    CGSize size = [UIScreen mainScreen].bounds.size;
    UIApplication *application = [UIApplication sharedApplication];
    if (UIInterfaceOrientationIsLandscape(orientation))
    {
        size = CGSizeMake(size.height, size.width);
    }
    if (application.statusBarHidden == NO)
    {
        size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);
    }
    return size;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
